<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      v-model:page="page"
      :permission="permissionList"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu="scope">
        <!-- <el-button type="primary" text icon="el-icon-view" @click.stop="handlePreview(scope.row.id)"
          >查看
        </el-button> -->
        <el-button
          type="primary"
          text
          icon="el-icon-edit"
          @click.stop="handlePrequalification(scope.row)"
          v-if="userCode && userCode.length == 6"
          >预审
        </el-button>
        <el-button
          type="primary"
          text
          icon="el-icon-edit"
          @click.stop="handleExamine(scope.row)"
          v-if="
            scope.row.status != 2 &&
            userCode &&
            userCode.length == 2 &&
            scope.row.prepareStatus == 2
          "
          >审核
        </el-button>
        <!-- <el-button
          type="primary"
          text
          icon="el-icon-edit"
          @click.stop="handleExamine(scope.row, '3')"
          v-if="scope.row.status == 1 && userCode && userCode.length == 2"
          >审核不通过
        </el-button> -->
        <el-button
          type="primary"
          text
          icon="el-icon-edit"
          @click.stop="handleSell(scope.row, '2')"
          v-if="scope.row.status == 2 && scope.row.isUp != 2 && userCode && userCode.length == 2"
          >上架
        </el-button>
        <el-button
          type="primary"
          text
          icon="el-icon-edit"
          @click.stop="handleDown(scope.row, '3')"
          v-if="scope.row.status == 2 && scope.row.isUp != 3 && userCode && userCode.length == 2"
          >下架
        </el-button>
      </template>
    </avue-crud>
  </basic-container>

  <el-dialog title="原因" v-model="dialogVisible" width="800px" :before-close="handleClose">
    <div class="d_box">
      <div class="d_row">
        <p class="d_p">下架原因：</p>
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入下架原因"
          v-model="opinion"
        >
        </el-input>
      </div>
      <div class="foot_box">
        <el-button @click="handleSubmit(false)">取消</el-button>
        <el-button @click="handleSubmit(true)" type="primary">确定</el-button>
      </div>
    </div>
  </el-dialog>

  <el-dialog :title="titleName" v-model="show2" width="800px" :before-close="handleCloseHear">
    <div class="d_box">
      <div class="d_row">
        <p class="d_p">意见：</p>
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入意见"
          v-model="opinion"
        >
        </el-input>
      </div>
      <div class="foot_box">
        <el-button @click="handleHearing(false)">不同意</el-button>
        <el-button @click="handleHearing(true)" type="primary">同意</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { list, update } from '@/api/project/list';
import { examine } from '@/api/organizational/organizationalReview';

import { mapGetters } from 'vuex';

export default {
  data() {
    return {
      form: {},
      selectionList: [],
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        indexLabel: '序号',
        indexWidth: '70',
        height: 'auto',
        calcHeight: 32,
        tip: false,
        searchShow: true,
        searchMenuSpan: 3,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        dialogClickModal: false,
        addBtn: false,
        header: false,
        delBtn: false,
        menu: true,
        column: [
          {
            label: '项目名称',
            prop: 'projectName',
            search: true,
            slot: true,
          },
          {
            label: '发起人',
            prop: 'creator',
          },
          {
            label: '项目地址',
            prop: 'projectAddr',
          },
          {
            label: '是否上架',
            prop: 'isUp',
            search: true,
            type: 'select',
            props: {
              label: 'name',
              value: 'value',
            },
            dicData: [
              { name: '未上架', value: 1 },
              { name: '上架', value: 2 },
              { name: '下架', value: 3 },
            ],
          },
          {
            label: '预审',
            prop: 'prepareStatus',
            search: true,
            type: 'select',
            props: {
              label: 'name',
              value: 'value',
            },
            dicData: [
              { name: '待审核', value: 1 },
              { name: '审核通过', value: 2 },
              { name: '审核不通过', value: 3 },
            ],
          },
          {
            label: '终审',
            prop: 'status',
            search: true,
            type: 'select',
            props: {
              label: 'name',
              value: 'value',
            },
            dicData: [
              { name: '待审核', value: 1 },
              { name: '审核通过', value: 2 },
              { name: '审核不通过', value: 3 },
            ],
          },
          {
            label: '项目有效开始时间',
            prop: 'projectStartTime',
          },
          {
            label: '项目类型',
            prop: 'projectType',
            search: false,
            type: 'select',
            props: {
              label: 'name',
              value: 'value',
            },
            dicUrl: '/blade-system/dict/child-list?current=1&size=10&parentId=1894952129912856578', // 假设这是获取字典的URL
            dicMethod: 'get',
            dicFormatter: res => {
              return res.data.map(item => {
                return {
                  name: item.dictValue,
                  value: item.dictKey,
                };
              });
            },
          },
          {
            label: '项目描述',
            prop: 'projectRemark',
          },
          //   {
          //     label: '项目图片首页',
          //     prop: 'attachLink',
          //   },
        ],
      },
      data: [],
      rowData: {},
      dialogVisible: false,
      opinion: '',
      userCode: '',
      userRole: '',
      titleName: '',
      show2: false,
    };
  },
  computed: {
    ...mapGetters(['userInfo', 'permission']),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: false,
        delBtn: true,
        editBtn: false,
      };
    },
  },
  methods: {
    handlePreview(name) {},
    handleDesign(name) {},
    handlePrequalification(row) {
      this.titleName = '预审';
      this.rowData = row;
      this.show2 = true;
    },
    async handleHearing(flag) {
      if (!this.opinion) {
        this.$message({
          type: 'error',
          message: '请输入意见!',
        });
        return;
      }
      if (flag) {
        let data = { ...this.rowData, reason: this.opinion };
        if (this.titleName == '预审') {
          data.prepareStatus = 2;
        } else {
          data.status = 2;
        }
        let res = await update(data);
        if (res.data.success == true) {
          this.show2 = false;
          this.opinion = '';
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        }
      } else {
        let data = { ...this.rowData, reason: this.opinion };
        if (this.titleName == '预审') {
          data.prepareStatus = 3;
        } else {
          data.status = 3;
        }
        let res = await update(data);
        if (res.data.success == true) {
          this.show2 = false;
          this.onLoad(this.page);
          this.opinion = '';
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        }
      }
    },
    handleExamine(row) {
      // update(row).then(res => {
      //   this.loading = false;
      //   this.$message.success(res.data.msg);
      //   this.selectionClear();
      // });
      this.titleName = '终审';
      this.rowData = row;
      this.show2 = true;
    },
    handleClose() {
      this.opinion = '';
      this.dialogVisible = false;
    },
    handleCloseHear() {
      this.opinion = '';
      this.show2 = false;
    },
    handleSell(row, type) {
      row.isUp = type;
      this.loading = true;
      update(row).then(res => {
        this.loading = false;
        this.$message.success(res.data.msg);
        this.selectionClear();
      });
    },
    handleDown(row, type) {
      this.rowData = row;
      this.dialogVisible = true;
    },
    async handleSubmit(flag) {
      if (flag) {
        if (!this.opinion) {
          this.$message({
            type: 'error',
            message: '请输入下架原因!',
          });
          return;
        }
        this.loading = true;
        //下架
        this.rowData.reason = this.opinion;
        this.rowData.isUp = 3;
        update(this.rowData).then(res => {
          this.loading = false;
          this.$message.success(res.data.msg);
          this.opinion = '';
          this.dialogVisible = false;
        });
      } else {
        this.dialogVisible = false;
      }
    },

    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      list(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
        console.log(res.data.msg);
        this.userCode = res.data.msg;
        // this.userCode = '360101';
        this.userRole = this.userInfo.role_name;
        console.log(this.userRole);
        this.option.menu = this.userCode.length != 4 && this.userRole != 'administrator';
      });
    },
  },
};
</script>

<style scoped lang="scss">
.d_box {
  padding: 0 20px;
  box-sizing: border-box;
  .d_row {
    display: flex;
    align-items: flex-start;
    margin: 18px 0;
    .d_p {
      width: 100px;
      margin: 0;
    }
  }
  .foot_box {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin: 20px 0;
  }
}
</style>
